package com.wgts.wgts.tools;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import static android.content.Context.TELEPHONY_SERVICE;

public class AppInfor {

    public static final String REFRESH_TOKEN = "refreshToken";

    private SharedpreferencesHelper sharedpreferencesHelper;

    public static final String PLAT_VERSION = "1.0";

    private String appVersion;

    private int appVersionCode;

    private String IMEI = "Android";

    private int vipFansCount;

    private int svipFansCount;
    //0:内部员工,1:普通会员，2:店小二，3，掌柜子，4：分舵主
    private int userState = -1;

    private String userName = "";

    private String userHead = "";

    private String userPhone = "";

    private String inviteCode = "";

    private String wChart = "";

    //可用余额
    private String availableBalance = "0";

    //判断是否有支付密码
    private boolean modifyPayPassword;

    //是否违规用户
    private int illegal;


    /**
     * 用户邀请码
     */
    private String userYQCode;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserHead() {
        return userHead;
    }

    public void setUserHead(String userHead) {
        this.userHead = userHead;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getAvailableBalance() {
        return availableBalance;
    }

    public void setAvailableBalance(String availableBalance) {
        this.availableBalance = availableBalance;
    }

    public String getInviteCode() {
        return inviteCode;
    }

    /**
     * 用户邀请码
     * @param inviteCode
     */
    public void setInviteCode(String inviteCode) {
        this.inviteCode = inviteCode;
    }

    public String getwChart() {
        return wChart;
    }

    public void setwChart(String wChart) {
        this.wChart = wChart;
    }

    public boolean isModifyPayPassword() {
        return modifyPayPassword;
    }

    public void setModifyPayPassword(boolean modifyPayPassword) {
        this.modifyPayPassword = modifyPayPassword;
    }

    private AppInfor() {
        appVersion = "";
    }

    private static class SingletonHolder {
        private static final AppInfor _INSTANCE = new AppInfor();
    }

    public int getVipFansCount() {
        return vipFansCount;
    }

    public void setVipFansCount(int vipFansCount) {
        this.vipFansCount = vipFansCount;
    }

    public int getSvipFansCount() {
        return svipFansCount;
    }

    public void setSvipFansCount(int svipFansCount) {
        this.svipFansCount = svipFansCount;
    }

    public static AppInfor getInstance() {
        return AppInfor.SingletonHolder._INSTANCE;
    }


    public AppInfor init(Context context, SharedpreferencesHelper sharedpreferencesHelper) {

        this.sharedpreferencesHelper = sharedpreferencesHelper;

        appVersionCode = 0;
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            appVersionCode = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionCode;

            appVersion = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


        return AppInfor.SingletonHolder._INSTANCE;

    }

    public String getAppVersion() {
        return appVersion;
    }

    public int getAppVersionCode() {
        return appVersionCode;
    }

    public String getIMEI() {
        return IMEI;
    }

    public void setIMEI(Context context) {

        if(!IMEI.equals("Android")) return;

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        String tempIMEI = ((TelephonyManager) context.getSystemService(TELEPHONY_SERVICE)).getDeviceId();

        if(tempIMEI != null && !tempIMEI.equals("")){
            IMEI = tempIMEI;
        }
    }

    private String accessToken = "";

    private String refreshToken = "";

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getRefreshToken() {
        if(refreshToken.equals("")){
            refreshToken = sharedpreferencesHelper.getString(REFRESH_TOKEN);
        }
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        sharedpreferencesHelper.write(REFRESH_TOKEN, refreshToken);
        this.refreshToken = refreshToken;
    }

    public int getUserState() {
        return userState;
    }

    public void setUserState(int userState) {
        this.userState = userState;
    }

    public String getUserYQCode() {
        return inviteCode;
    }

    public void setUserYQCode(String userYQCode) {
        this.inviteCode = userYQCode;
    }

    public int getIllegal() {
        return illegal;
    }

    public void setIllegal(int illegal) {
        this.illegal = illegal;
    }
}
